package com.example.demo1014;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private IMyAidlInterface myAidlInterface;

    private IMyAidlInterface2 iMyAidlInterface2;

    private ServiceConnection connection = new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            Log.d(TAG, "onServiceConnected: IBinder service = " + service);

            myAidlInterface = IMyAidlInterface.Stub.asInterface(service);

            Log.d(TAG, "onServiceConnected: myAidlInterface = " + myAidlInterface);

            // 确保在绑定成功后调用 AIDL 方法
             callAidlMethod();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

            myAidlInterface = null;
        }
    };

    private ServiceConnection connection2 = new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            Log.d(TAG, "onServiceConnected: IBinder service = " + service);

            iMyAidlInterface2 = IMyAidlInterface2.Stub.asInterface(service);

            Log.d(TAG, "onServiceConnected: myAidlInterface2 = " + iMyAidlInterface2);

            // 确保在绑定成功后调用 AIDL 方法
            callAidlMethod2();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

            myAidlInterface = null;
            iMyAidlInterface2 = null;
        }
    };

    private void callAidlMethod() {
        // 检查 myAidlInterface 是否为 null
        if (myAidlInterface != null) {
            try {
                // 调用 AIDL 方法
                myAidlInterface.myMethod(42);
                // 假设还有一个 getString 方法
                String response = myAidlInterface.getString();
                Log.d(TAG, "Response from AIDL method: " + response);
            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException occurred while calling AIDL method", e);
            }
        } else {
            Log.e(TAG, "myAidlInterface is null, service may not be bound.");
        }
    }

    private void callAidlMethod2() {
        // 检查 myAidlInterface 是否为 null
        if (iMyAidlInterface2 != null) {
            try {
                // 调用 AIDL 方法
                iMyAidlInterface2.sum(5,6);

            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException occurred while calling AIDL method", e);
            }
        } else {
            Log.e(TAG, "myAidlInterface is null, service may not be bound.");
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        Intent intent = new Intent(this, MyAidlService.class);
        bindService(intent, connection, Context.BIND_AUTO_CREATE);

        Intent intent2 = new Intent(this, MyService2.class);
        bindService(intent2, connection2, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    @Override
    protected void onStop() {
        super.onStop();
        unbindService(connection);
    }
}